#include "eqibcontrol.h"
#include "eqibwebpage.h"
#include "ui_eqibcontrol.h"

EQIBControl::EQIBControl(QWidget *parent) :
    QTabWidget(parent),
    ui(new Ui::EQIBControl)
{

    QWebSettings *settings = QWebSettings::globalSettings();
    settings->setAttribute(QWebSettings::PluginsEnabled, true);
    settings->setAttribute(QWebSettings::JavascriptEnabled, true);
    settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
    settings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
    settings->setAttribute(QWebSettings::PrintElementBackgrounds, true);
    settings->setAttribute(QWebSettings::JavascriptCanCloseWindows, true);
    settings->setAttribute(QWebSettings::DnsPrefetchEnabled, true);
    //settings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    settings->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
    settings->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
    settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
    settings->setAttribute(QWebSettings::NotificationsEnabled, true);
    settings->setAttribute(QWebSettings::AutoLoadImages, true);
    settings->setAttribute(QWebSettings::JavaEnabled, true);


    QNetworkProxyFactory::setUseSystemConfiguration(true);




    this->cookieJar = new QNetworkCookieJar();
    this->nam = new QNetworkAccessManager();
    this->nam->setCookieJar(this->cookieJar);


    QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    QWebSettings::setIconDatabasePath(location);

    settings->setDefaultTextEncoding("UTF8");

    ui->setupUi(this);
    //this->setWindowIcon(QIcon(":/resources/imgs/app_logo.ico"));

    connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(on_tabClose(int)));

    EQIBUtils::get()->settings->beginGroup("position");
    int w = EQIBUtils::get()->settings->value("width",QVariant(1300)).toString().toInt();
    int h= EQIBUtils::get()->settings->value("height",QVariant(600)).toString().toInt();
    int x = EQIBUtils::get()->settings->value("top",QVariant(0)).toString().toInt();
    int y = EQIBUtils::get()->settings->value("left",QVariant(0)).toString().toInt();
    EQIBUtils::get()->settings->endGroup();
    this->resize(QSize(w,h));
    this->move(x,y);
}

EQIBControl::~EQIBControl()
{
    qDebug() << "destroy control...";
    this->hide();
    for(int i=0;i<this->count();i++){
        QWebView* webView = qobject_cast<QWebView*>(this->widget(i));
        if(webView != NULL){
            delete webView->page();
        }
    }
    delete this->cookieJar;
    delete this->nam;
    delete ui;
}

void EQIBControl::addPage(const QString url){
    qDebug() << "add page : " << url;
    this->createPage(url)->webView->load(QUrl(url));
}

void EQIBControl::on_tabClose(int tabIndex){
    QWebView* webView = qobject_cast<QWebView*>(this->widget(tabIndex));

    if(webView != NULL){
        this->removeTab(tabIndex);
        delete webView->page();
    }
    if(this->count()==0){
        this->close();
    }
}

void EQIBControl::closeEvent(QCloseEvent *event)
{
    qDebug() << "onClosing... save window position...";
    QSize size = this->size();
    QPoint pos = this->pos();
    EQIBUtils::get()->settings->beginGroup("position");
    EQIBUtils::get()->settings->setValue("width",QVariant(size.width()));
    EQIBUtils::get()->settings->setValue("height",QVariant(size.height()));
    EQIBUtils::get()->settings->setValue("top",QVariant(pos.x()));
    EQIBUtils::get()->settings->setValue("left",QVariant(pos.y()));
    EQIBUtils::get()->settings->endGroup();
    QTabWidget::closeEvent(event);
}

EQIBWebPage * EQIBControl::createPage(QString title){
    QWebView * webView = new QWebView(this);
    EQIBWebPage * page = new EQIBWebPage(webView,this);
    page->setNetworkAccessManager(this->nam);
    webView->setPage(page);
    int tabIndex = this->addTab(webView,title);
    this->setCurrentIndex(tabIndex);
    return page;
}
